

package com.hazelcast.internal.monitor.impl;

import java.util.concurrent.atomic.AtomicLongFieldUpdater;

import static java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater;

/**
 * The implementation of internal indexes stats specialized for global indexes.
 * <p>
 * The main trait of the implementation is the concurrency support, which is
 * required for global indexes because they are shared among partitions.
 */
public class GlobalIndexesStats implements IndexesStats {

    private static final AtomicLongFieldUpdater<GlobalIndexesStats> QUERY_COUNT = newUpdater(GlobalIndexesStats.class, "queryCount");
    private static final AtomicLongFieldUpdater<GlobalIndexesStats> INDEXED_QUERY_COUNT = newUpdater(GlobalIndexesStats.class, "indexedQueryCount");
    private static final AtomicLongFieldUpdater<GlobalIndexesStats> INDEXES_SKIPPED_QUERY_COUNT = newUpdater(GlobalIndexesStats.class, "indexesSkippedQueryCount");
    private static final AtomicLongFieldUpdater<GlobalIndexesStats> NO_MATCHING_INDEX_QUERY_COUNT = newUpdater(GlobalIndexesStats.class, "noMatchingIndexQueryCount");

    private volatile long queryCount;
    private volatile long indexedQueryCount;
    private volatile long indexesSkippedQueryCount;
    private volatile long noMatchingIndexQueryCount;

    @Override
    public long getQueryCount() {
        return queryCount;
    }

    @Override
    public void incrementQueryCount() {
        QUERY_COUNT.incrementAndGet(this);
    }

    @Override
    public long getIndexedQueryCount() {
        return indexedQueryCount;
    }

    @Override
    public void incrementIndexedQueryCount() {
        INDEXED_QUERY_COUNT.incrementAndGet(this);
    }

    @Override
    public long getIndexesSkippedQueryCount() {
        return indexesSkippedQueryCount;
    }

    @Override
    public void incrementIndexesSkippedQueryCount() {
        INDEXES_SKIPPED_QUERY_COUNT.incrementAndGet(this);
    }

    @Override
    public long getNoMatchingIndexQueryCount() {
        return noMatchingIndexQueryCount;
    }

    @Override
    public void incrementNoMatchingIndexQueryCount() {
        NO_MATCHING_INDEX_QUERY_COUNT.incrementAndGet(this);
    }

    @Override
    public PerIndexStats createPerIndexStats(boolean ordered, boolean usesCachedQueryableEntries) {
        return new GlobalPerIndexStats(ordered, usesCachedQueryableEntries);
    }

}
